﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using FileBrowser.Exceptions;

namespace FileBrowser.Classes
{
    internal class FolderDealer
    {
        /// <summary>
        /// 返回给定路径的文件夹下的string文件名列表（绝对路径）
        /// </summary>
        /// <param name="path">文件夹的绝对路径</param>
        /// <returns>List<string>类型的文件列表，包括文件夹和文件</returns>
        /// <exception cref="AccessPermissionException"></exception>
        public static List<string> GetFilesOfFolder(string path)
        {
            if (!Directory.Exists(path))
                return null;

            List<string> result = new List<string>();
            DirectoryInfo directoryInfo = new DirectoryInfo(path);
            try
            {
                foreach (var item in directoryInfo.GetDirectories())
                    result.Add(item.FullName);
                foreach (var item in directoryInfo.GetFiles())
                    result.Add(item.FullName);
            }catch(Exception)
            {
                throw new AccessPermissionException(path);
            }
            return result;
        }

        public static void OpenFile(string path)
        {
            if (Path.GetExtension(path).Equals(".txt"))
                OpenTXTFile(path);
            else if (Path.GetExtension(path).Equals(".exe"))
                OpenEXEFile(path);
        }

        /// <summary>
        /// 通过记事本（notepad.exe）打开指定路径的.txt文件
        /// </summary>
        /// <param name="path">.txt文件的绝对路径</param>
        private static void OpenTXTFile(string path)
        {
            Process.Start("notepad.exe", path);
        }

        /// <summary>
        /// 打开指定路径的.exe文件
        /// </summary>
        /// <param name="path">.exe文件的绝对路径</param>
        private static void OpenEXEFile(string path)
        {
            Process.Start(path);
        }
    }
}
